package com.ctgu.rabbitmq.service.impl;

import com.ctgu.rabbitmq.entity.UsersEntity;
import com.ctgu.rabbitmq.rabbitmq.exchange.alternateExchange.AlternateExchangeConfig;
import com.ctgu.rabbitmq.rabbitmq.exchange.defaultExchange.DefaultExchangeConfig;
import com.ctgu.rabbitmq.rabbitmq.exchange.directExchange.DirectExchangeConfig;
import com.ctgu.rabbitmq.rabbitmq.exchange.fanoutExchange.FanoutExchangeConfig;
import com.ctgu.rabbitmq.rabbitmq.exchange.headersExchange.HeadersExchangeConfig;
import com.ctgu.rabbitmq.rabbitmq.exchange.topicExchange.TopicExchangeConfig;
import com.ctgu.rabbitmq.service.ExchangeService;
import com.ctgu.rabbitmq.utils.ApiResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * @ ClassName ExchangeServiceImpl
 * @ Author Alex2
 * @ Date 2025/8/15 8:57
 **/
@Log4j2
@Service
public class ExchangeServiceImpl implements ExchangeService {

    private static final String TAG = "Alex";

    private final RabbitTemplate rabbitTemplate;

    public ExchangeServiceImpl(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    /**
     * @ Author: Alex
     * @ Description: 默认交换机
     */
    @Override
    public ApiResult defaultExchange() {
        String msg = "Hello World";
        Message message = new Message(msg.getBytes());
        rabbitTemplate.send("", DefaultExchangeConfig.QUEUE_DEFAULT, message);
        log.debug("{} : 默认交换机发送消息到队列成功 : {}", TAG, msg);
        return ApiResult.success("默认交换机发送消息到队列", msg);
    }

    /**
     * @ Author: Alex
     * @ Description: 扇出交换机
     */
    @Override
    public ApiResult fanoutExchange() {
        String msg = "Hello World";
        MessageProperties props = new MessageProperties();
        props.setExpiration("5000");
        Message message = new Message(msg.getBytes(), props);
        rabbitTemplate.send(FanoutExchangeConfig.FANOUT_EXCHANGE, "", message);
        log.debug("{} : 扇出交换机发送消息成功 : {}", TAG, msg);
        return ApiResult.success("扇出交换机发送消息成功", msg);
    }

    /**
     * @ Author: Alex
     * @ Description: 直连交换机
     */
    @Override
    public ApiResult directExchange() {
        UsersEntity usersEntity = new UsersEntity(1, "Alex", null);
        rabbitTemplate.convertAndSend(
                DirectExchangeConfig.DIRECT_EXCHANGE,
                DirectExchangeConfig.ROUTING_KEY_INFO,
                usersEntity,
                message -> {
                    message.getMessageProperties().setExpiration(String.valueOf(5000));
                    return message;
                }
        );
        log.debug("{} : 直连交换机向 info 路由发送消息成功 : {}", TAG, usersEntity);
        return ApiResult.success("直连交换机向 info 路由发送消息成功", usersEntity);
    }

    /**
     * @ Author: Alex
     * @ Description: 主题交换机
     */
    @Override
    public ApiResult topicExchange() {
        String msg = "Hello World";
        Message messageOrder = new Message(msg.getBytes());
        rabbitTemplate.send(TopicExchangeConfig.TOPIC_EXCHANGE, "order.create", messageOrder);
        log.debug("{} : 主题交换机向 order.create 路由发送消息成功 : {}", TAG, msg);
        return ApiResult.success("主题交换机向 order.create 路由发送消息成功", msg);
    }

    /**
     * @ Author: Alex
     * @ Description: 头部交换机
     */
    @Override
    public ApiResult headersExchange() {
        String msg = "Hello World";
        MessageProperties properties = new MessageProperties();
        properties.setHeader("department", "HR");
        properties.setHeader("level", "senior");
        Message message = new Message(msg.getBytes(), properties);
        rabbitTemplate.send(HeadersExchangeConfig.HEADERS_EXCHANGE, "", message);
        log.debug("{} : 头部交换机向 HR 发送消息成功 : {}", TAG, msg);
        return ApiResult.success("头部交换机向 HR 发送消息成功", msg);
    }

    /**
     * @ Author: Alex
     * @ Description: 备用交换机
     */
    @Override
    public ApiResult alternateExchange() {
        String msg = "Hello World";
        rabbitTemplate.convertAndSend(
                AlternateExchangeConfig.MAIN_EXCHANGE, // 主交换机
                "errorKey",                            // 错误的 routingKey
                msg
        );
        log.debug("{} : 向主交换机发送错误的 routingKey : {}", TAG, msg);
        return ApiResult.success("消息已发送: " + msg);
    }
}
